package com.zj.ecology

import android.util.Log
import com.zj.base.LayoutModule
import com.zj.base.base_act.BaseRequestActivity
import com.zj.base.httpclien.HttpClient
import com.zj.base.list.adapters.BaseAdapterDataSet
import com.zj.base.list.holders.BaseViewHolder
import com.zj.base.list.views.EmptyRecyclerView
import com.zj.base.permission.annotations.CheekPermissions
import com.zj.base.permission.manifests.PermissionsManifest
import com.zj.base.views.BaseLoadingView.DisplayMode
import com.zj.base.refresh.interfaces.RefreshLayoutIn
import com.zj.base.refresh.listeners.OnRefreshLoadMoreListener
import com.zj.ecology.list.MoreCommentInfo
import com.zj.ecology.test.TestTools
import kotlinx.android.synthetic.main.activity_main.*

@CheekPermissions(isForce = [false, true, false], value = *[PermissionsManifest.READ_CONTACTS, PermissionsManifest.CAMERA, PermissionsManifest.FINE_LOCATION])
class MainActivity : BaseRequestActivity() {

    val url = "http://219.143.6.77:8888/creditAPP/core/article/getNexts"
    var main_rv: EmptyRecyclerView<MoreCommentInfo.HomeCommentInfo>? = null
    var curPage = 1
    var hasCache = false

    override fun setTitle(): String {
        return "自搭建框架测试"

    }

    override fun getContentView(): LayoutModule {
        return LayoutModule(R.layout.activity_main, false, false)
    }

    val s = arrayOf("", "", "")
    override fun initView() {
        main_rv = find(R.id.main_rv)
        hint(DisplayMode.loading, "加载中，请稍候...", true)
        getData(HttpClient.DataType.REFRESH)
    }

    override fun initListener() {
        main_refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayoutIn: RefreshLayoutIn) {
                getData(HttpClient.DataType.REFRESH)
            }

            override fun onLoadMore(refreshLayoutIn: RefreshLayoutIn) {
                getData(HttpClient.DataType.LOADMORE)
            }
        })
    }

    override fun callRefresh() {
        hint(DisplayMode.loading, "加载中，请稍候...", true)
        getData(HttpClient.DataType.REFRESH)
    }

    fun getData(type: HttpClient.DataType) {
        when (type) {
            HttpClient.DataType.REFRESH -> {
                curPage = 1
            }
            HttpClient.DataType.LOADMORE -> {
                curPage++
            }
            else -> {
                throw IllegalStateException("you should not use httpDataType.Cache to send a http request");
            }
        }
        val getEnable = (main_rv == null || main_rv?.adapter == null || main_rv?.adapter?.itemCount == 0)
        HttpClient.post(9090, this@MainActivity, MoreCommentInfo::class.java).params(TestTools.getEncryptParams("page", curPage)).url(url).cacheEnable(getEnable, type == HttpClient.DataType.REFRESH).dataType(type).send()
    }

    override fun onHttpDataReceived(data: Any?, requstCode: Int, dataType: HttpClient.DataType) {
        if (dataType == HttpClient.DataType.REFRESH) {
            main_refreshLayout.refreshCompleted()
        }
        if (dataType == HttpClient.DataType.LOADMORE) {
            main_refreshLayout.loadMoreCompleted()
        }
        Log.e("onHttpDataReceived", "" + data + "   " + requstCode)
        when (requstCode) {
            9090 -> {
                if (dataType == HttpClient.DataType.CACHE) hasCache = true
                val info: MoreCommentInfo = data as MoreCommentInfo
                main_refreshLayout.setNoMoreData(info.isEnd)
                main_rv?.setData(R.layout.item_ladapter, dataType == HttpClient.DataType.LOADMORE, info.commentInfos, object : BaseAdapterDataSet<MoreCommentInfo.HomeCommentInfo>() {
                    override fun initData(holder: BaseViewHolder?, position: Int, module: MoreCommentInfo.HomeCommentInfo?) {
                        holder?.setText(R.id.item_tvName, module?.title)
                        holder?.setImage(R.id.item_iv, module?.imageLink)
                    }
                })
                hint(DisplayMode.normal, "", false)
            }
        }
    }

    override fun onHttpError(msg: String?, errorCode: Int, reqCode: Int, type: HttpClient.DataType?) {
        when (reqCode) {
            9090 -> {
                if (type == HttpClient.DataType.REFRESH) {
                    main_refreshLayout.refreshCompleted()
                }
                if (type == HttpClient.DataType.LOADMORE) {
                    main_refreshLayout.loadMoreCompleted()
                }
                if (hasCache || (main_rv != null && main_rv?.adapter != null && main_rv!!.adapter.itemCount > 0))
                    hint(DisplayMode.normal, "", false)
                else {
                    hint(DisplayMode.noNetwork, "数据异常", false)
                }
            }
        }
    }
}